//
//  HTStatus.swift
//  mySinaBlog
//
//  Created by whtapple on 3/27/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit
//微博模型
class HTStatus: NSObject {
///创建时间
    var created_at: String?
     ///微博 ID
    var id: Int64 = 0
    ///微博信息内容
    var text: String?
    ///微博来源
    var source: String?
    ///微博配图
    var pic_urls: [[String: AnyObject]]?{
        didSet{
            //根据得到的图片数组，来拿到一个只存有 URL 的数组
            //等于0，说明没有图片，直接返回
            if pic_urls?.count == 0{
                return
            }
            //不等于0 的话，就可以新建一个数组了
            storePic = [NSURL]()
            //遍历数组中的字典
            for dict in pic_urls!{
                //守卫，看能不能拿到 string
                guard let url = dict["thumbnail_pic"] as? String else{
                    print("没有拿到 url")
                   continue
                }
                //拿到 string 后，看能不能转化为 URL
                guard let picURL = NSURL(string: url) else{
                    continue
                }
                //转化后存储
                storePic?.append(picURL)
            }
            
        }
    }
    var storePic:[NSURL]?
    ///转发数
    var reposts_count: Int = 0
    ///评论数
    var comments_count: Int = 0
    ///表态数
    var attitudes_count: Int = 0
    ///用户信息
    var user: HTUsers?
    ///字典转模型
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    //重写 setValue：forKey 方法，给 user赋值时能变成模型，而不是一个字典
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            //可选绑定
            if let dict = value as? [String : AnyObject]{
              user = HTUsers(dict: dict)
            }
            return
        }
        //加这句是为了当关键字不是 user时调用
        super.setValue(value, forKey: key)
    }
    //没有 key时调用
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    //重写 description
    override var description: String {
        let key = ["created_at","id","text","source","pic_urls","reposts_count","comments_count","attitudes_count","user"]
        return "\n\t\(dictionaryWithValuesForKeys(key))"
    }
}
